<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
	protected function _initDb() {
        $dbOption = $this->getOption('resources');
	    $dbOption = $dbOption['db'];
	
	    // Setup database
	    $db = Zend_Db::factory($dbOption['adapter'], $dbOption['params']);
	
	    $db->setFetchMode(Zend_Db::FETCH_ASSOC);
	    $db->query("SET NAMES 'utf8'");
	    $db->query("SET CHARACTER SET 'utf8'");
	
	    Zend_Registry::set('connectDB', $db);
	
	    //Khi thiet lap che do nay model moi co the su dung duoc
	    Zend_Db_Table::setDefaultAdapter($db);
	    // Return it, so that it can be stored by the bootstrap
	    
	    return $db;
	}
	protected function _initTemplate()
	{
		$db=Zend_Registry::get('connectDB');
		$sql = "select * from templates";		
		$result = $db->fetchAll($sql);
		$default = 'Default';
		foreach ($result as $value) {
			if($value['status'] == 1)
				$default = $value['name'];
		}
		Zend_Registry::set('template', $default);
	}
	protected function _initLanguage()
	{
		$db=Zend_Registry::get('connectDB');
		$sql = "select * from language";
		$result = $db->fetchAll($sql);
		$default = 'en';
		foreach ($result as $value) {
			if($value['status'] == 1)
				$default = $value['id'];
		}
		Zend_Registry::set('language', $default);
	}
	protected function _initMenu()
	{
		Zend_Registry::set('menu', '2');
	}
	protected function _initRoutes()
	{		
		$db = Zend_Registry::get('connectDB');
		$sql = "select id from language";
		$result = $db->fetchAll($sql);
		
		
		$frontController = Zend_Controller_Front::getInstance();
		$router = $frontController->getRouter();
		$router->removeDefaultRoutes();
		$router->addRoute(
		'langmodulecontrolleraction',
		new Zend_Controller_Router_Route("/:lang/:module/:controller/:action",
		array('lang' => Zend_Registry::get('language'))));
		$router->addRoute(
		'7',
		new Zend_Controller_Router_Route('/:controller',
		array('lang' => Zend_Registry::get('language'),
			  'module' => 'default',
			  'controller' => 'index',
			  'action' => 'index'		
		))		
		);
				$router->addRoute(
		'8',
		new Zend_Controller_Router_Route('/:controller/:action',
		array('lang' => Zend_Registry::get('language'),
			  'module' => 'default',
			  'controller' => 'index',
			  'action' => 'index'		
		))		
		);
		foreach ($result as $value) {			
			$router->addRoute(
			'1'.$value['id'],
			new Zend_Controller_Router_Route("/".$value['id']."/:module/:controller/",
			array('lang' => $value['id'],
				  'module' => 'default',
				  'controller' => 'index',
				  'action' => 'index'	
			))		
			);
			$router->addRoute(
			'2'.$value['id'],
			new Zend_Controller_Router_Route('/'.$value['id'].'/:module',
			array('lang' => $value['id'],
				  'module' => 'default',
				  'controller' => 'index',
				  'action' => 'index'		
			))		
			);
			$router->addRoute(
			'1.1'.$value['id'],
			new Zend_Controller_Router_Route("/".$value['id']."/:controller/:action",
			array('lang' => $value['id'],
				  'module' => 'default',
				  'controller' => 'index',
				  'action' => 'index'	
			))		
			);
			$router->addRoute(
			'2.1'.$value['id'],
			new Zend_Controller_Router_Route('/'.$value['id'].'/:controller',
			array('lang' => $value['id'],
				  'module' => 'default',
				  'controller' => 'index',
				  'action' => 'index'		
			))		
			);
			$router->addRoute(
			'3'.$value['id'],
			new Zend_Controller_Router_Route('/'.$value['id'],
			array('lang' => $value['id'],
				  'module' => 'default',
				  'controller' => 'index',
				  'action' => 'index'		
			))		
			);
		}
		$router->addRoute(
		'1',
		new Zend_Controller_Router_Route('/admin',
		array('lang' => Zend_Registry::get('language'),
			  'module' => 'admin',
			  'controller' => 'index',
			  'action' => 'index'		
		))		
		);
		$router->addRoute(
		'2',
		new Zend_Controller_Router_Route('/admin/:controller',
		array('lang' => Zend_Registry::get('language'),
			  'module' => 'admin',
			  'controller' => 'index',
			  'action' => 'index'		
		))		
		);
		$router->addRoute(
		'3',
		new Zend_Controller_Router_Route('/admin/:controller/:action',
		array('lang' => Zend_Registry::get('language'),
			  'module' => 'admin',
			  'controller' => 'index',
			  'action' => 'index'		
		))		
		);
		$router->addRoute(
		'4',
		new Zend_Controller_Router_Route('/default',
		array('lang' => Zend_Registry::get('language'),
			  'module' => 'default',
			  'controller' => 'index',
			  'action' => 'index'		
		))		
		);
		$router->addRoute(
		'5',
		new Zend_Controller_Router_Route('/default/:controller',
		array('lang' => Zend_Registry::get('language'),
			  'module' => 'default',
			  'controller' => 'index',
			  'action' => 'index'		
		))		
		);
		$router->addRoute(
		'6',
		new Zend_Controller_Router_Route('/default/:controller/:action',
		array('lang' => Zend_Registry::get('language'),
			  'module' => 'default',
			  'controller' => 'index',
			  'action' => 'index'		
		))		
		);
	}
	protected function _initAutoload()
	{
		$autoloader = new Zend_Application_Module_Autoloader(array(
			'namespace' => '',
			'basePath'  => APPLICATION_PATH
		));
		return $autoloader;
	}
}